﻿namespace SCCM_Client_Injector_2014
{
    using Microsoft.ConfigurationManagement.ManagementProvider;
    using Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine;
    using Microsoft.Win32;
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Collections.Generic;
    using System.Deployment.Application;
    using System.IO;
    using System.Management;
    using System.Management.Instrumentation;
    using Microsoft.ConfigurationManagement;
    using System.Runtime.InteropServices;
    using System.Security.Principal;
    using System.Security.Permissions;


    public class Main : Form
    {
        // Private
        //private IContainer components;
        private GroupBox groupBoxCollections;
        private WqlConnectionManager WMIConnection;
        private CheckedListBox checkedListBoxCollectionsMembership;
        private CheckedListBox checkListBoxCollections;
        private TextBox textBoxFilterCollections;
        private TextBox textBoxSiteServer;
        private Label label1;
        private Label labelServer;
        private Label labelClient;
        private Label labelClientName;
        private Label labelMembershipClientName;
        private RadioButton radioButtonStartsWith;
        private RadioButton radioButtonIsExactly;
        private RadioButton radioButtonContains;
        private Button buttonUncheckCollections;
        private Button buttonShowAllCollections;
        private Button buttonRemoveClientfromtheCollections;
        private Button buttonAddClienttoCollection;
        private Button buttonFilter;
        private Button buttonShowCollectionMemberships;
        private Button buttonConnect;
        private Button buttonCheckExists;
        private Button buttonCheckDuplicates;
        private Button buttonClearClientList;
        private Button buttonMultiLineEntry;
        private Button buttonPopExplorer;
        private Button buttonPopCompMgmt;
        private Button buttonRepairSCCM;
        private MenuStrip menuStripMain;
        private ToolStripMenuItem menuToolStripMenuItem;
        private ToolStripMenuItem optionsToolStripMenuItem;
        private ToolStripMenuItem exitToolStripMenuItem;
        private ToolStripMenuItem helpToolStripMenuItem;
        private ToolStripMenuItem helpToolStripMenuItem1;
        private ToolStripMenuItem aboutToolStripMenuItem;
        private ToolStripMenuItem licenseToolStripMenuItem;
        private string password = "";
        private string userName = "";
        private string computer = "";
        List<string> ClientListSplit = new List<string>();
        public TextBox textBoxClientName;
        public Button buttonViewLog;


        //Public Static
        public static string WindowProgramTitle = "SCCM Client Injector 2014";
        public static string WindowTitle = "";
        public static string DuplicateURL;
        public static string MultiClientList;
        public static List<string> ClientListResults = new List<string>();


        public Main()
        {
            this.InitializeComponent();
            this.textBoxFilterCollections.Text = "";
            RegistryKey key = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Callahan Consulting\SCCM Client Injector 2014");
            this.computer = (string) key.GetValue("Server");
            this.textBoxSiteServer.Text = this.computer;
            OptionsForm.boolAutoConnect = Properties.Settings.Default.AutoConnect;
            OptionsForm.boolLoggingEnabled = Properties.Settings.Default.LoggingEnabled;
            OptionsForm.boolLogTimeStamp = Properties.Settings.Default.LogTimeStamp;
            OptionsForm.boolLogClientID = Properties.Settings.Default.LogClientID;
            OptionsForm.boolLogClientName = Properties.Settings.Default.LogClientName;
            OptionsForm.boolLogPkgID = Properties.Settings.Default.LogPkgID;
            OptionsForm.boolLogPkgName = Properties.Settings.Default.LogPkgName;
            OptionsForm.stringLogFilePath = Properties.Settings.Default.LogFilePath;
            OptionsForm.stringSCCMRepairFolder = Properties.Settings.Default.SCCMRepairFolder;
            OptionsForm.stringClientRepairCommand = Properties.Settings.Default.ClientRepairCommand;

            if (OptionsForm.boolAutoConnect == true) {ConnectServer();}
            if (OptionsForm.boolLoggingEnabled == true) { buttonViewLog.Enabled = true; } else if (OptionsForm.boolLoggingEnabled == false) { buttonViewLog.Enabled = false; }

        }

        // This creates our GUI...I reccomend changing only via the [Design] tab.  YMMV.
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Main));
            this.groupBoxCollections = new System.Windows.Forms.GroupBox();
            this.buttonClearClientList = new System.Windows.Forms.Button();
            this.labelClientName = new System.Windows.Forms.Label();
            this.labelMembershipClientName = new System.Windows.Forms.Label();
            this.buttonShowCollectionMemberships = new System.Windows.Forms.Button();
            this.buttonUncheckCollections = new System.Windows.Forms.Button();
            this.buttonShowAllCollections = new System.Windows.Forms.Button();
            this.buttonRemoveClientfromtheCollections = new System.Windows.Forms.Button();
            this.buttonAddClienttoCollection = new System.Windows.Forms.Button();
            this.buttonFilter = new System.Windows.Forms.Button();
            this.radioButtonIsExactly = new System.Windows.Forms.RadioButton();
            this.radioButtonContains = new System.Windows.Forms.RadioButton();
            this.radioButtonStartsWith = new System.Windows.Forms.RadioButton();
            this.textBoxFilterCollections = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.checkedListBoxCollectionsMembership = new System.Windows.Forms.CheckedListBox();
            this.checkListBoxCollections = new System.Windows.Forms.CheckedListBox();
            this.labelServer = new System.Windows.Forms.Label();
            this.textBoxSiteServer = new System.Windows.Forms.TextBox();
            this.labelClient = new System.Windows.Forms.Label();
            this.textBoxClientName = new System.Windows.Forms.TextBox();
            this.buttonConnect = new System.Windows.Forms.Button();
            this.buttonCheckExists = new System.Windows.Forms.Button();
            this.buttonCheckDuplicates = new System.Windows.Forms.Button();
            this.buttonMultiLineEntry = new System.Windows.Forms.Button();
            this.menuStripMain = new System.Windows.Forms.MenuStrip();
            this.menuToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.optionsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.helpToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.helpToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
            this.aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.licenseToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.buttonViewLog = new System.Windows.Forms.Button();
            this.buttonRepairSCCM = new System.Windows.Forms.Button();
            this.buttonPopCompMgmt = new System.Windows.Forms.Button();
            this.buttonPopExplorer = new System.Windows.Forms.Button();
            this.groupBoxCollections.SuspendLayout();
            this.menuStripMain.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBoxCollections
            // 
            this.groupBoxCollections.Controls.Add(this.buttonClearClientList);
            this.groupBoxCollections.Controls.Add(this.labelClientName);
            this.groupBoxCollections.Controls.Add(this.labelMembershipClientName);
            this.groupBoxCollections.Controls.Add(this.buttonShowCollectionMemberships);
            this.groupBoxCollections.Controls.Add(this.buttonUncheckCollections);
            this.groupBoxCollections.Controls.Add(this.buttonShowAllCollections);
            this.groupBoxCollections.Controls.Add(this.buttonRemoveClientfromtheCollections);
            this.groupBoxCollections.Controls.Add(this.buttonAddClienttoCollection);
            this.groupBoxCollections.Controls.Add(this.buttonFilter);
            this.groupBoxCollections.Controls.Add(this.radioButtonIsExactly);
            this.groupBoxCollections.Controls.Add(this.radioButtonContains);
            this.groupBoxCollections.Controls.Add(this.radioButtonStartsWith);
            this.groupBoxCollections.Controls.Add(this.textBoxFilterCollections);
            this.groupBoxCollections.Controls.Add(this.label1);
            this.groupBoxCollections.Controls.Add(this.checkedListBoxCollectionsMembership);
            this.groupBoxCollections.Controls.Add(this.checkListBoxCollections);
            this.groupBoxCollections.Location = new System.Drawing.Point(12, 74);
            this.groupBoxCollections.Name = "groupBoxCollections";
            this.groupBoxCollections.Size = new System.Drawing.Size(1066, 326);
            this.groupBoxCollections.TabIndex = 0;
            this.groupBoxCollections.TabStop = false;
            this.groupBoxCollections.Text = "Collections";
            // 
            // buttonClearClientList
            // 
            this.buttonClearClientList.Image = ((System.Drawing.Image)(resources.GetObject("buttonClearClientList.Image")));
            this.buttonClearClientList.ImageAlign = System.Drawing.ContentAlignment.BottomRight;
            this.buttonClearClientList.Location = new System.Drawing.Point(494, 183);
            this.buttonClearClientList.Name = "buttonClearClientList";
            this.buttonClearClientList.Size = new System.Drawing.Size(75, 40);
            this.buttonClearClientList.TabIndex = 14;
            this.buttonClearClientList.Text = "Clear &List";
            this.buttonClearClientList.UseVisualStyleBackColor = true;
            this.buttonClearClientList.Click += new System.EventHandler(this.buttonClearClientList_Click);
            // 
            // labelClientName
            // 
            this.labelClientName.AutoSize = true;
            this.labelClientName.Location = new System.Drawing.Point(575, 21);
            this.labelClientName.Name = "labelClientName";
            this.labelClientName.Size = new System.Drawing.Size(112, 13);
            this.labelClientName.TabIndex = 15;
            this.labelClientName.Text = "Memberships of client:";
            // 
            // labelMembershipClientName
            // 
            this.labelMembershipClientName.AutoSize = true;
            this.labelMembershipClientName.ForeColor = System.Drawing.Color.Teal;
            this.labelMembershipClientName.Location = new System.Drawing.Point(684, 21);
            this.labelMembershipClientName.Name = "labelMembershipClientName";
            this.labelMembershipClientName.Size = new System.Drawing.Size(10, 13);
            this.labelMembershipClientName.TabIndex = 14;
            this.labelMembershipClientName.Text = "-";
            // 
            // buttonShowCollectionMemberships
            // 
            this.buttonShowCollectionMemberships.Image = global::SCCM_Client_Injector_2014.Properties.Resources.db_status_a;
            this.buttonShowCollectionMemberships.ImageAlign = System.Drawing.ContentAlignment.BottomRight;
            this.buttonShowCollectionMemberships.Location = new System.Drawing.Point(494, 137);
            this.buttonShowCollectionMemberships.Name = "buttonShowCollectionMemberships";
            this.buttonShowCollectionMemberships.Size = new System.Drawing.Size(75, 40);
            this.buttonShowCollectionMemberships.TabIndex = 13;
            this.buttonShowCollectionMemberships.Text = "&Show Client\r\nMembership";
            this.buttonShowCollectionMemberships.UseVisualStyleBackColor = true;
            this.buttonShowCollectionMemberships.Click += new System.EventHandler(this.buttonShowCollectionMemberships_Click);
            // 
            // buttonUncheckCollections
            // 
            this.buttonUncheckCollections.Image = global::SCCM_Client_Injector_2014.Properties.Resources.document_revert_5a;
            this.buttonUncheckCollections.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.buttonUncheckCollections.Location = new System.Drawing.Point(494, 278);
            this.buttonUncheckCollections.Name = "buttonUncheckCollections";
            this.buttonUncheckCollections.Size = new System.Drawing.Size(75, 40);
            this.buttonUncheckCollections.TabIndex = 16;
            this.buttonUncheckCollections.Text = "&Uncheck Collections";
            this.buttonUncheckCollections.UseVisualStyleBackColor = true;
            this.buttonUncheckCollections.Click += new System.EventHandler(this.buttonUncheckCollections_Click);
            // 
            // buttonShowAllCollections
            // 
            this.buttonShowAllCollections.Image = global::SCCM_Client_Injector_2014.Properties.Resources.tools_wizard_2a;
            this.buttonShowAllCollections.ImageAlign = System.Drawing.ContentAlignment.TopLeft;
            this.buttonShowAllCollections.Location = new System.Drawing.Point(494, 232);
            this.buttonShowAllCollections.Name = "buttonShowAllCollections";
            this.buttonShowAllCollections.Size = new System.Drawing.Size(75, 40);
            this.buttonShowAllCollections.TabIndex = 15;
            this.buttonShowAllCollections.Text = "Sho&w All Collections";
            this.buttonShowAllCollections.UseVisualStyleBackColor = true;
            this.buttonShowAllCollections.Click += new System.EventHandler(this.buttonShowAllCollections_Click);
            // 
            // buttonRemoveClientfromtheCollections
            // 
            this.buttonRemoveClientfromtheCollections.Image = global::SCCM_Client_Injector_2014.Properties.Resources.go_jump_4b;
            this.buttonRemoveClientfromtheCollections.ImageAlign = System.Drawing.ContentAlignment.BottomLeft;
            this.buttonRemoveClientfromtheCollections.Location = new System.Drawing.Point(494, 91);
            this.buttonRemoveClientfromtheCollections.Name = "buttonRemoveClientfromtheCollections";
            this.buttonRemoveClientfromtheCollections.Size = new System.Drawing.Size(75, 40);
            this.buttonRemoveClientfromtheCollections.TabIndex = 12;
            this.buttonRemoveClientfromtheCollections.Text = "&Remove Client";
            this.buttonRemoveClientfromtheCollections.UseVisualStyleBackColor = true;
            this.buttonRemoveClientfromtheCollections.Click += new System.EventHandler(this.buttonRemoveClientfromtheCollections_Click);
            // 
            // buttonAddClienttoCollection
            // 
            this.buttonAddClienttoCollection.Image = ((System.Drawing.Image)(resources.GetObject("buttonAddClienttoCollection.Image")));
            this.buttonAddClienttoCollection.ImageAlign = System.Drawing.ContentAlignment.TopRight;
            this.buttonAddClienttoCollection.Location = new System.Drawing.Point(494, 45);
            this.buttonAddClienttoCollection.Name = "buttonAddClienttoCollection";
            this.buttonAddClienttoCollection.Size = new System.Drawing.Size(75, 40);
            this.buttonAddClienttoCollection.TabIndex = 11;
            this.buttonAddClienttoCollection.Text = "&Add \r\nClient";
            this.buttonAddClienttoCollection.UseVisualStyleBackColor = true;
            this.buttonAddClienttoCollection.Click += new System.EventHandler(this.buttonAddClienttoCollection_Click);
            // 
            // buttonFilter
            // 
            this.buttonFilter.Location = new System.Drawing.Point(494, 15);
            this.buttonFilter.Name = "buttonFilter";
            this.buttonFilter.Size = new System.Drawing.Size(75, 23);
            this.buttonFilter.TabIndex = 10;
            this.buttonFilter.Text = "&Filter";
            this.buttonFilter.UseVisualStyleBackColor = true;
            this.buttonFilter.Click += new System.EventHandler(this.buttonFilter_Click);
            // 
            // radioButtonIsExactly
            // 
            this.radioButtonIsExactly.AutoSize = true;
            this.radioButtonIsExactly.Location = new System.Drawing.Point(420, 19);
            this.radioButtonIsExactly.Name = "radioButtonIsExactly";
            this.radioButtonIsExactly.Size = new System.Drawing.Size(52, 17);
            this.radioButtonIsExactly.TabIndex = 9;
            this.radioButtonIsExactly.TabStop = true;
            this.radioButtonIsExactly.Text = "Exac&t";
            this.radioButtonIsExactly.UseVisualStyleBackColor = true;
            this.radioButtonIsExactly.CheckedChanged += new System.EventHandler(this.radioButtonIsExactly_CheckedChanged);
            // 
            // radioButtonContains
            // 
            this.radioButtonContains.AutoSize = true;
            this.radioButtonContains.Checked = true;
            this.radioButtonContains.Location = new System.Drawing.Point(261, 19);
            this.radioButtonContains.Name = "radioButtonContains";
            this.radioButtonContains.Size = new System.Drawing.Size(66, 17);
            this.radioButtonContains.TabIndex = 7;
            this.radioButtonContains.TabStop = true;
            this.radioButtonContains.Text = "Co&ntains";
            this.radioButtonContains.UseVisualStyleBackColor = true;
            this.radioButtonContains.CheckedChanged += new System.EventHandler(this.radioButtonContains_CheckedChanged);
            // 
            // radioButtonStartsWith
            // 
            this.radioButtonStartsWith.AutoSize = true;
            this.radioButtonStartsWith.Location = new System.Drawing.Point(344, 19);
            this.radioButtonStartsWith.Name = "radioButtonStartsWith";
            this.radioButtonStartsWith.Size = new System.Drawing.Size(52, 17);
            this.radioButtonStartsWith.TabIndex = 8;
            this.radioButtonStartsWith.Text = "Sta&rts";
            this.radioButtonStartsWith.UseVisualStyleBackColor = true;
            this.radioButtonStartsWith.CheckedChanged += new System.EventHandler(this.radioButtonStartsWith_CheckedChanged);
            // 
            // textBoxFilterCollections
            // 
            this.textBoxFilterCollections.Location = new System.Drawing.Point(41, 18);
            this.textBoxFilterCollections.Name = "textBoxFilterCollections";
            this.textBoxFilterCollections.Size = new System.Drawing.Size(214, 20);
            this.textBoxFilterCollections.TabIndex = 6;
            this.textBoxFilterCollections.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnKeyDownHandlerFilter);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(6, 21);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(29, 13);
            this.label1.TabIndex = 2;
            this.label1.Text = "Filter";
            // 
            // checkedListBoxCollectionsMembership
            // 
            this.checkedListBoxCollectionsMembership.BackColor = System.Drawing.Color.Beige;
            this.checkedListBoxCollectionsMembership.CheckOnClick = true;
            this.checkedListBoxCollectionsMembership.FormattingEnabled = true;
            this.checkedListBoxCollectionsMembership.Location = new System.Drawing.Point(578, 44);
            this.checkedListBoxCollectionsMembership.Name = "checkedListBoxCollectionsMembership";
            this.checkedListBoxCollectionsMembership.Size = new System.Drawing.Size(482, 274);
            this.checkedListBoxCollectionsMembership.Sorted = true;
            this.checkedListBoxCollectionsMembership.TabIndex = 18;
            this.checkedListBoxCollectionsMembership.SelectedIndexChanged += new System.EventHandler(this.checkListBoxCollections_SelectedIndexChanged);
            // 
            // checkListBoxCollections
            // 
            this.checkListBoxCollections.BackColor = System.Drawing.Color.Beige;
            this.checkListBoxCollections.CheckOnClick = true;
            this.checkListBoxCollections.FormattingEnabled = true;
            this.checkListBoxCollections.Location = new System.Drawing.Point(6, 44);
            this.checkListBoxCollections.Name = "checkListBoxCollections";
            this.checkListBoxCollections.Size = new System.Drawing.Size(482, 274);
            this.checkListBoxCollections.Sorted = true;
            this.checkListBoxCollections.TabIndex = 17;
            // 
            // labelServer
            // 
            this.labelServer.AutoSize = true;
            this.labelServer.Location = new System.Drawing.Point(18, 27);
            this.labelServer.Name = "labelServer";
            this.labelServer.Size = new System.Drawing.Size(41, 13);
            this.labelServer.TabIndex = 1;
            this.labelServer.Text = "Server:";
            // 
            // textBoxSiteServer
            // 
            this.textBoxSiteServer.BackColor = System.Drawing.Color.MistyRose;
            this.textBoxSiteServer.Location = new System.Drawing.Point(66, 24);
            this.textBoxSiteServer.Name = "textBoxSiteServer";
            this.textBoxSiteServer.Size = new System.Drawing.Size(164, 20);
            this.textBoxSiteServer.TabIndex = 0;
            this.textBoxSiteServer.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnKeyDownHandlerServer);
            // 
            // labelClient
            // 
            this.labelClient.AutoSize = true;
            this.labelClient.Location = new System.Drawing.Point(18, 54);
            this.labelClient.Name = "labelClient";
            this.labelClient.Size = new System.Drawing.Size(36, 13);
            this.labelClient.TabIndex = 3;
            this.labelClient.Text = "Client:";
            // 
            // textBoxClientName
            // 
            this.textBoxClientName.Location = new System.Drawing.Point(66, 51);
            this.textBoxClientName.Name = "textBoxClientName";
            this.textBoxClientName.Size = new System.Drawing.Size(164, 20);
            this.textBoxClientName.TabIndex = 2;
            this.textBoxClientName.TextChanged += new System.EventHandler(this.textBoxClientName_TextChanged_1);
            this.textBoxClientName.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnKeyDownHandlerClient);
            // 
            // buttonConnect
            // 
            this.buttonConnect.Location = new System.Drawing.Point(236, 22);
            this.buttonConnect.Name = "buttonConnect";
            this.buttonConnect.Size = new System.Drawing.Size(103, 23);
            this.buttonConnect.TabIndex = 1;
            this.buttonConnect.Text = "&Connect";
            this.buttonConnect.UseVisualStyleBackColor = true;
            this.buttonConnect.Click += new System.EventHandler(this.buttonConnect_Click);
            // 
            // buttonCheckExists
            // 
            this.buttonCheckExists.Enabled = false;
            this.buttonCheckExists.Location = new System.Drawing.Point(345, 49);
            this.buttonCheckExists.Name = "buttonCheckExists";
            this.buttonCheckExists.Size = new System.Drawing.Size(103, 23);
            this.buttonCheckExists.TabIndex = 5;
            this.buttonCheckExists.Text = "Check E&xists";
            this.buttonCheckExists.UseVisualStyleBackColor = true;
            this.buttonCheckExists.Click += new System.EventHandler(this.buttonCheckExists_Click);
            // 
            // buttonCheckDuplicates
            // 
            this.buttonCheckDuplicates.Location = new System.Drawing.Point(575, 49);
            this.buttonCheckDuplicates.Name = "buttonCheckDuplicates";
            this.buttonCheckDuplicates.Size = new System.Drawing.Size(103, 23);
            this.buttonCheckDuplicates.TabIndex = 4;
            this.buttonCheckDuplicates.Text = "Check &Duplicates";
            this.buttonCheckDuplicates.UseVisualStyleBackColor = true;
            this.buttonCheckDuplicates.Visible = false;
            this.buttonCheckDuplicates.Click += new System.EventHandler(this.buttonCheckDuplicates_Click);
            // 
            // buttonMultiLineEntry
            // 
            this.buttonMultiLineEntry.Enabled = false;
            this.buttonMultiLineEntry.Location = new System.Drawing.Point(236, 49);
            this.buttonMultiLineEntry.Name = "buttonMultiLineEntry";
            this.buttonMultiLineEntry.Size = new System.Drawing.Size(103, 23);
            this.buttonMultiLineEntry.TabIndex = 3;
            this.buttonMultiLineEntry.Text = "Multi-line Entry";
            this.buttonMultiLineEntry.UseVisualStyleBackColor = true;
            this.buttonMultiLineEntry.Click += new System.EventHandler(this.buttonMultiLineEntry_Click);
            // 
            // menuStripMain
            // 
            this.menuStripMain.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.menuStripMain.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.menuToolStripMenuItem,
            this.helpToolStripMenuItem});
            this.menuStripMain.Location = new System.Drawing.Point(0, 0);
            this.menuStripMain.Name = "menuStripMain";
            this.menuStripMain.Size = new System.Drawing.Size(1090, 24);
            this.menuStripMain.TabIndex = 11;
            this.menuStripMain.Text = "Menu bar";
            // 
            // menuToolStripMenuItem
            // 
            this.menuToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.optionsToolStripMenuItem,
            this.exitToolStripMenuItem});
            this.menuToolStripMenuItem.Name = "menuToolStripMenuItem";
            this.menuToolStripMenuItem.Size = new System.Drawing.Size(46, 20);
            this.menuToolStripMenuItem.Text = "Menu";
            // 
            // optionsToolStripMenuItem
            // 
            this.optionsToolStripMenuItem.Name = "optionsToolStripMenuItem";
            this.optionsToolStripMenuItem.Size = new System.Drawing.Size(110, 22);
            this.optionsToolStripMenuItem.Text = "&Options";
            this.optionsToolStripMenuItem.Click += new System.EventHandler(this.optionsToolStripMenuItem_Click);
            // 
            // exitToolStripMenuItem
            // 
            this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
            this.exitToolStripMenuItem.Size = new System.Drawing.Size(110, 22);
            this.exitToolStripMenuItem.Text = "E&xit";
            this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
            // 
            // helpToolStripMenuItem
            // 
            this.helpToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.helpToolStripMenuItem1,
            this.aboutToolStripMenuItem,
            this.licenseToolStripMenuItem});
            this.helpToolStripMenuItem.Name = "helpToolStripMenuItem";
            this.helpToolStripMenuItem.Size = new System.Drawing.Size(41, 20);
            this.helpToolStripMenuItem.Text = "&Help";
            // 
            // helpToolStripMenuItem1
            // 
            this.helpToolStripMenuItem1.Name = "helpToolStripMenuItem1";
            this.helpToolStripMenuItem1.Size = new System.Drawing.Size(111, 22);
            this.helpToolStripMenuItem1.Text = "&Help";
            this.helpToolStripMenuItem1.Click += new System.EventHandler(this.helpToolStripMenuItem1_Click);
            // 
            // aboutToolStripMenuItem
            // 
            this.aboutToolStripMenuItem.Name = "aboutToolStripMenuItem";
            this.aboutToolStripMenuItem.Size = new System.Drawing.Size(111, 22);
            this.aboutToolStripMenuItem.Text = "&About";
            this.aboutToolStripMenuItem.Click += new System.EventHandler(this.aboutToolStripMenuItem_Click);
            // 
            // licenseToolStripMenuItem
            // 
            this.licenseToolStripMenuItem.Name = "licenseToolStripMenuItem";
            this.licenseToolStripMenuItem.Size = new System.Drawing.Size(111, 22);
            this.licenseToolStripMenuItem.Text = "&License";
            this.licenseToolStripMenuItem.Click += new System.EventHandler(this.licenseToolStripMenuItem_Click);
            // 
            // buttonViewLog
            // 
            this.buttonViewLog.BackgroundImage = global::SCCM_Client_Injector_2014.Properties.Resources.application_view_list;
            this.buttonViewLog.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
            this.buttonViewLog.Location = new System.Drawing.Point(975, 49);
            this.buttonViewLog.Name = "buttonViewLog";
            this.buttonViewLog.Size = new System.Drawing.Size(103, 23);
            this.buttonViewLog.TabIndex = 15;
            this.buttonViewLog.Text = "View Log...";
            this.buttonViewLog.UseVisualStyleBackColor = true;
            this.buttonViewLog.Click += new System.EventHandler(this.buttonViewLog_Click);
            // 
            // buttonRepairSCCM
            // 
            this.buttonRepairSCCM.BackgroundImage = global::SCCM_Client_Injector_2014.Properties.Resources.edit_add_4;
            this.buttonRepairSCCM.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
            this.buttonRepairSCCM.Location = new System.Drawing.Point(345, 22);
            this.buttonRepairSCCM.Name = "buttonRepairSCCM";
            this.buttonRepairSCCM.Size = new System.Drawing.Size(103, 23);
            this.buttonRepairSCCM.TabIndex = 14;
            this.buttonRepairSCCM.Text = "Repair SCCM";
            this.buttonRepairSCCM.UseVisualStyleBackColor = true;
            this.buttonRepairSCCM.Click += new System.EventHandler(this.buttonRepairSCCM_Click);
            // 
            // buttonPopCompMgmt
            // 
            this.buttonPopCompMgmt.BackgroundImage = global::SCCM_Client_Injector_2014.Properties.Resources.lightning_go;
            this.buttonPopCompMgmt.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
            this.buttonPopCompMgmt.Location = new System.Drawing.Point(454, 49);
            this.buttonPopCompMgmt.Name = "buttonPopCompMgmt";
            this.buttonPopCompMgmt.Size = new System.Drawing.Size(115, 23);
            this.buttonPopCompMgmt.TabIndex = 13;
            this.buttonPopCompMgmt.Text = "Open Mgmt";
            this.buttonPopCompMgmt.UseVisualStyleBackColor = true;
            this.buttonPopCompMgmt.Click += new System.EventHandler(this.buttonPopCompMgmt_Click);
            // 
            // buttonPopExplorer
            // 
            this.buttonPopExplorer.BackgroundImage = global::SCCM_Client_Injector_2014.Properties.Resources.application_side_tree;
            this.buttonPopExplorer.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
            this.buttonPopExplorer.Location = new System.Drawing.Point(454, 22);
            this.buttonPopExplorer.Name = "buttonPopExplorer";
            this.buttonPopExplorer.Size = new System.Drawing.Size(115, 23);
            this.buttonPopExplorer.TabIndex = 12;
            this.buttonPopExplorer.Text = "Open Explorer";
            this.buttonPopExplorer.UseVisualStyleBackColor = true;
            this.buttonPopExplorer.Click += new System.EventHandler(this.buttonPopExplorer_Click);
            // 
            // Main
            // 
            this.ClientSize = new System.Drawing.Size(1090, 404);
            this.Controls.Add(this.buttonViewLog);
            this.Controls.Add(this.buttonRepairSCCM);
            this.Controls.Add(this.buttonPopCompMgmt);
            this.Controls.Add(this.buttonPopExplorer);
            this.Controls.Add(this.buttonMultiLineEntry);
            this.Controls.Add(this.buttonCheckDuplicates);
            this.Controls.Add(this.buttonCheckExists);
            this.Controls.Add(this.buttonConnect);
            this.Controls.Add(this.textBoxClientName);
            this.Controls.Add(this.labelClient);
            this.Controls.Add(this.textBoxSiteServer);
            this.Controls.Add(this.labelServer);
            this.Controls.Add(this.groupBoxCollections);
            this.Controls.Add(this.menuStripMain);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "Main";
            this.Text = "SCCM Client Injector 2014";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Main_FormClosing);
            this.Load += new System.EventHandler(this.Main_Load);
            this.groupBoxCollections.ResumeLayout(false);
            this.groupBoxCollections.PerformLayout();
            this.menuStripMain.ResumeLayout(false);
            this.menuStripMain.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        // The next three functions trap the <ENTER> key so we can use it contextually
        private void OnKeyDownHandlerServer(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //enter key has been pressed
                // trigger the 'button'
                ConnectServer();
                Application.DoEvents();
            }
        }

        private void OnKeyDownHandlerClient(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //enter key has been pressed
                // trigger the 'button'
                string Exists = CheckExists();
                Application.DoEvents();
                if (Exists == "TRUE")
                {
                    ShowMemberships();
                    Application.DoEvents();
                }
            }
        }

        private void OnKeyDownHandlerFilter(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //enter key has been pressed
                //  trigger the 'button'
                FilterList();
                Application.DoEvents();
            }
        }
        // End of <ENTER> catches

        //Click Event for the button labeled 'Add Client'
        private void buttonAddClienttoCollection_Click(object sender, EventArgs e)
        {
            if (this.WMIConnection != null)
            {
                ClientListSplit.Clear();
                if (textBoxClientName.Text != "Multiple Clients")
                {
                    if (textBoxClientName.Text != "")
                    {
                        ClientListSplit.Add(textBoxClientName.Text.ToUpper());
                    }
                    else
                    {
                        MessageBox.Show("Please define a Client Name.");
                        LogFile.LogEvent("NULL", 0, "", "", "ERR", "No client name found in textBoxClientName");
                        textBoxClientName.Focus();
                    }
                }
                else if (textBoxClientName.Text == "Multiple Clients")
                {
                    // We have multiple entries; split based on new line, 
                    string[] ClientList = MultiClientList.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
                    foreach (string ClientName in ClientList)
                    {
                            ClientListSplit.Add(ClientName.ToUpper());
                    }
                }


                // We now have our array with client name(s)
                // Adding to collection is trivial at this point
                foreach (string client in ClientListSplit)                    
                {
                    new AddClienttoCollections(this.WMIConnection).AddClient(this.checkListBoxCollections, client);
                    Application.DoEvents();
                }

            }

            else
            {
                this.textBoxSiteServer.BackColor = System.Drawing.Color.MistyRose;
                MessageBox.Show("Please connect first.");
                LogFile.LogEvent("NULL", 0, "", "", "DBG", "Not connected to server");
                textBoxSiteServer.Focus();
            }

        }

        // Click Event for button labeled "Filter"
        private void buttonFilter_Click(object sender, EventArgs e)
        {
            FilterList();
        }

        // This is the process for filtering the collections list.
        // We keep it seperate from the Click Event handler so we can trigger it via
        // other methods, such as an <ENTER> key.  We will follow this same process
        // for many other Click Event handlers and multiple triggers (including future ones)
        private void FilterList()
        {
            if (this.WMIConnection != null)
            {
                string text = this.textBoxFilterCollections.Text;
                if (text == "")
                {
                    MessageBox.Show("Please enter a Collection filter name.");
                    textBoxFilterCollections.Focus();
                }
                else
                {
                    LoadCollections collections = new LoadCollections(this.WMIConnection);

                    // Our filtering options (*foo, *foo* and foo)
                    if (this.radioButtonStartsWith.Checked)
                    {
                        collections.ListCollectionsWithFilter(this.checkListBoxCollections, text, "StartsWith");
                        Application.DoEvents();
                    }

                    else if (this.radioButtonContains.Checked)
                    {
                        collections.ListCollectionsWithFilter(this.checkListBoxCollections, text, "Contains");
                        Application.DoEvents();
                    }

                    else if (this.radioButtonIsExactly.Checked)
                    {
                        collections.ListCollectionsWithFilter(this.checkListBoxCollections, text, "IsExactly");
                        Application.DoEvents();
                    }
                }
            }
            else
            {
                this.textBoxSiteServer.BackColor = System.Drawing.Color.MistyRose;
                MessageBox.Show("Please connect first.");
                textBoxSiteServer.Focus();
            }
        }

        // Click Event handler for the button labeled 'Remove Client'
        // This currently handles the handler as well as the process.
        private void buttonRemoveClientfromtheCollections_Click(object sender, EventArgs e)
        {
            if (this.WMIConnection != null)
            {
                if (textBoxClientName.Text != "Multiple Clients")
                {
                    if (textBoxClientName.Text != "")
                    {
                        ClientListSplit.Add(textBoxClientName.Text.ToUpper());
                    }
                    else
                    {
                        MessageBox.Show("Please define a Client Name.");
                        LogFile.LogEvent("NULL", 0, "", "", "ERR", "No client name found in textBoxClientName");
                        textBoxClientName.Focus();
                    }
                }
                else if (textBoxClientName.Text == "Multiple Clients")
                {
                    // We have multiple entries; split based on new line, 
                    string[] ClientList = MultiClientList.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
                    foreach (string ClientName in ClientList)
                    {
                        ClientListSplit.Add(ClientName.ToUpper());
                    }
                }


                // We now have our array with client name(s)
                // Adding to collection is trivial at this point
                foreach (string client in ClientListSplit)
                {
                    new DeleteClientfromCollections(this.WMIConnection).RemoveClient(this.checkedListBoxCollectionsMembership, client);
                    Application.DoEvents();
                }

            }

            else
            {
                this.textBoxSiteServer.BackColor = System.Drawing.Color.MistyRose;
                MessageBox.Show("Please connect first.");
                LogFile.LogEvent("NULL", 0, "", "", "DBG", "Not connected to server");
                textBoxSiteServer.Focus();
            }

        }


        // Click Event handler for refreshing the list of collections
        private void buttonShowAllCollections_Click(object sender, EventArgs e)
        {
            if (this.WMIConnection != null)
            {
                this.textBoxFilterCollections.Text = "";
                new LoadCollections(this.WMIConnection).ListCollections(this.checkListBoxCollections);
                Application.DoEvents();
            }
            else
            {
                this.textBoxSiteServer.BackColor = System.Drawing.Color.MistyRose;
                MessageBox.Show("Please connect first.");
                textBoxSiteServer.Focus();
            }

        }

        // Click Event handler for the button labeled 'Show Client Membership'
        private void buttonShowCollectionMemberships_Click(object sender, EventArgs e)
        {
            ShowMemberships();
            Application.DoEvents();
        }

        // Process for showing client memberships
        private void ShowMemberships()
        {
            if (this.WMIConnection != null)
            {
                string client = textBoxClientName.Text.ToUpper();
                if (client != "")
                {
                    new CollectionMemberships(this.WMIConnection).ListMemberships(this.checkedListBoxCollectionsMembership, client, this.labelMembershipClientName);
                    Application.DoEvents();
                }
                else
                {
                    MessageBox.Show("Please define a Client Name.");
                    textBoxClientName.Focus();
                }
            }
            else
            {
                this.textBoxSiteServer.BackColor = System.Drawing.Color.MistyRose;
                MessageBox.Show("Please connect first.");
                textBoxSiteServer.Focus();
            }
        }
     
        private void buttonUncheckCollections_Click(object sender, EventArgs e)
        {
            this.checkListBoxCollections.ClearSelected();
            IEnumerator enumerator = this.checkListBoxCollections.CheckedIndices.GetEnumerator();
            while (enumerator.MoveNext())
            {
                this.checkListBoxCollections.SetItemChecked((int)enumerator.Current, false);
            }


            this.checkedListBoxCollectionsMembership.ClearSelected();
            IEnumerator enumerator2 = this.checkedListBoxCollectionsMembership.CheckedIndices.GetEnumerator();
            while (enumerator2.MoveNext())
            {
                this.checkedListBoxCollectionsMembership.SetItemChecked((int)enumerator2.Current, false);
            }

        }

        private void checkListBoxCollections_SelectedIndexChanged(object sender, EventArgs e)
        {
            string str = this.checkListBoxCollections.SelectedItem.ToString();
            int selectedIndex = this.checkListBoxCollections.SelectedIndex;
            if (str.Contains(" - (Query based membership)"))
            {
                MessageBox.Show("Membership rule type is not \"Direct\"!");
                this.checkListBoxCollections.SetItemChecked(selectedIndex, false);
            }
            try
            {
                this.checkListBoxCollections.SelectedItems.Clear();
            }
            catch
            {
            }

        }

        private void buttonConnect_Click(object sender, EventArgs e)
        {
            ConnectServer();
        }
        
        private void ConnectServer ()
        {
            this.computer = this.textBoxSiteServer.Text;
            Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Callahan Consulting\SCCM Client Injector 2014").SetValue("Server", this.computer);
            this.WMIConnection = new WMIConnect().Connect(this.computer, this.userName, this.password);
            if (this.WMIConnection != null)
            {
                this.textBoxSiteServer.BackColor = System.Drawing.Color.Honeydew;
                new LoadCollections(this.WMIConnection).ListCollections(this.checkListBoxCollections);
                Application.DoEvents();
                this.buttonAddClienttoCollection.Enabled = true;
                this.buttonUncheckCollections.Enabled = true;
                this.buttonFilter.Enabled = true;
                this.buttonShowCollectionMemberships.Enabled = true;
                this.buttonShowAllCollections.Enabled = true;
                this.buttonRemoveClientfromtheCollections.Enabled = true;
                this.buttonCheckExists.Enabled = true;
                this.buttonCheckDuplicates.Enabled = true;
                this.buttonMultiLineEntry.Enabled = true;
            }
        }

        private void buttonCheckDuplicates_Click(object sender, EventArgs e)
        {
            // CheckDuplicates();
            ClientDuplicateForm frm = new ClientDuplicateForm(); 
            Application.DoEvents();
            frm.Show();
        }

        private void CheckDuplicates()
        {
                
                ModifyHTML();
                DuplicateURL = AppDomain.CurrentDomain.BaseDirectory + "DuplicateCheck.html";
                WindowTitle = "Duplicate Check - " + WindowProgramTitle;
                BrowserForm frm = new BrowserForm();
                Application.DoEvents();
                frm.Show();
                Application.DoEvents();
        }

        private void buttonCheckExists_Click(object sender, EventArgs e)
        {
            string Nothing = CheckExists();
        }

        private string CheckExists()
        {
            if (this.WMIConnection != null)
            {
                string client = textBoxClientName.Text.ToUpper();
                if (client != "")
                {
                    int integerValue = 0;
                    string query = "SELECT ResourceId FROM SMS_R_System WHERE NetbiosName ='" + textBoxClientName.Text + "'";
                    foreach (IResultObject Result in this.WMIConnection.QueryProcessor.ExecuteQuery(query))
                    {
                        integerValue = Result["ResourceID"].IntegerValue;
                    }
                    Application.DoEvents();
                    if (integerValue == 0)
                    {
                        textBoxClientName.BackColor = System.Drawing.Color.MistyRose;
                        string Exists = "FALSE";
                        return Exists;
                    }
                    else
                    {
                        textBoxClientName.BackColor = System.Drawing.Color.Honeydew;
                        string Exists = "TRUE";
                        return Exists;
                    }
                }
                else
                {
                    MessageBox.Show("Please define a Client Name.");
                    textBoxClientName.BackColor = System.Drawing.Color.MistyRose;
                    string Exists = "FALSE";
                    textBoxClientName.Focus();
                    return Exists;
                }
            }
            else
            {
                this.textBoxSiteServer.BackColor = System.Drawing.Color.MistyRose;
                textBoxClientName.BackColor = System.Drawing.Color.MistyRose;
                MessageBox.Show("Please connect first.");
                textBoxSiteServer.Focus();
                string Exists = "FALSE";
                return Exists;
            }
            
        }

        private void radioButtonContains_CheckedChanged(object sender, EventArgs e)
        {
            this.textBoxFilterCollections.Focus();
        }

        private void radioButtonStartsWith_CheckedChanged(object sender, EventArgs e)
        {
            this.textBoxFilterCollections.Focus();
        }

        private void radioButtonIsExactly_CheckedChanged(object sender, EventArgs e)
        {
            this.textBoxFilterCollections.Focus();
        }

        private void ModifyHTML()
        {
            int line_to_edit_client = 86; //Set line to overwrite for client name
            int line_to_edit_server = 53; //Set line to overwrite for server name
            int line_to_edit_server2 = 225; //Set line to overwrite for servername URL
            string destinationFile = AppDomain.CurrentDomain.BaseDirectory + "DuplicateCheck.html";

            // Set the new data to write
            string lineToWriteClient = "txtUserName.Value = \"" + textBoxClientName.Text + "\"";
            string lineToWriteServer = "strSMSServer = \"" + textBoxSiteServer.Text + "\"";
            string lineToWriteServerPostback = "oXMLHTTP.open \"POST\",\"http://" + textBoxSiteServer.Text + "/Tools/sccm.asmx/DeleteComputerByID\",false";

            // Read the old file.
            string[] lines = File.ReadAllLines(destinationFile);

            // Write the new file over the old file. **Warning** for ease of editing the html by line number, this is a 1-based counter
            using (StreamWriter writer = new StreamWriter(destinationFile))
            {
                for (int currentLine = 1; currentLine <= lines.Length; ++currentLine)
                {
                    if (currentLine == line_to_edit_client)
                    {
                        writer.WriteLine(lineToWriteClient);
                    }
                    else if (currentLine == line_to_edit_server)
                    {
                        writer.WriteLine(lineToWriteServer);
                    }
                    else if (currentLine == line_to_edit_server2)
                    {
                        writer.WriteLine(lineToWriteServerPostback);
                    }
                    else
                    {
                        writer.WriteLine(lines[currentLine - 1]);
                    }
                }
            }
        }

        private void Main_Load(object sender, EventArgs e)
        {

        }

        private void buttonClearClientList_Click(object sender, EventArgs e)
        {
            checkedListBoxCollectionsMembership.Items.Clear();
            labelMembershipClientName.Text = "-";
            // textBoxClientName.Text = "";
            // this.textBoxClientName.BackColor = System.Drawing.Color.MistyRose;
        }

        private void buttonMultiLineEntry_Click(object sender, EventArgs e)
        {
            MultiClientForm frm = new MultiClientForm();
            frm.ShowDialog(this);
            textBoxClientName.Text = "Multiple Clients";
            textBoxClientName.BackColor = System.Drawing.Color.LightGoldenrodYellow;
            buttonCheckExists.Enabled = false;
        }

        private void textBoxSiteServer_TextChanged(object sender, EventArgs e)
        {
            this.textBoxSiteServer.BackColor = System.Drawing.Color.MistyRose;
        }

        private void textBoxClientName_TextChanged_1(object sender, EventArgs e)
        {
            if (textBoxClientName.Text != "Multiple Clients")
            {
                buttonCheckExists.Enabled = true;
                textBoxClientName.BackColor = System.Drawing.Color.White;
            }
        }

        private void helpToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
            {
                System.Deployment.Application.ApplicationDeployment cd = System.Deployment.Application.ApplicationDeployment.CurrentDeployment;
                DuplicateURL = AppDomain.CurrentDomain.BaseDirectory + "help.html";
                WindowTitle = "Help - " + WindowProgramTitle;
                BrowserForm frm = new BrowserForm();
                Application.DoEvents();
                frm.Show();
            }
            else
            {
                WindowTitle = "Help - " + WindowProgramTitle;
                DuplicateURL = AppDomain.CurrentDomain.BaseDirectory + "help.html";
                BrowserForm frm = new BrowserForm();
                Application.DoEvents();
                frm.Show();
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Deployment.Application.ApplicationDeployment cd = System.Deployment.Application.ApplicationDeployment.CurrentDeployment;
            MessageBox.Show("Find us on SourceForge.net! https://sourceforge.net/projects/sccm2014/", "SCCM Client Injector 2014 v3.2." + cd.CurrentVersion.Revision + " (beta)");
        }

        private void licenseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WindowTitle = "Licensing - " + WindowProgramTitle;
            DuplicateURL = "http://www.gnu.org/licenses/gpl-3.0-standalone.html";
            BrowserForm frm = new BrowserForm();
            Application.DoEvents();
            frm.Show();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OptionsForm frm = new OptionsForm();
            frm.Show();
        }

        private void buttonPopExplorer_Click(object sender, EventArgs e)
        {
            Process.Start("explorer", "/e,/root,\\\\" + textBoxClientName.Text + "\\C$");
            LogFile.LogEvent(textBoxClientName.Text, 0, "", "", "OTH", "Opened Explorer: \\\\" + textBoxClientName.Text + "\\C$");
        }

        private void buttonPopCompMgmt_Click(object sender, EventArgs e)
        {
            Process.Start("compmgmt.msc", "/computer:" + textBoxClientName.Text);
            LogFile.LogEvent(textBoxClientName.Text, 0, "", "", "OTH", "Opened compmgmt.msc");
        }

        private void buttonRepairSCCM_Click(object sender, EventArgs e)
        {
            // Copy the folder
                string ClientName = "";
                if (textBoxClientName.Text != "Multiple Clients")
                {
                    if (textBoxClientName.Text != "")
                    {
                        ClientName = textBoxClientName.Text.ToUpper();
                    }
                    else
                    {
                        MessageBox.Show("Please define a Client Name.  \n\rMultiple client use is not currently supported.");
                        LogFile.LogEvent("NULL", 0, "", "", "ERR", "No client name found in textBoxClientName: buttonRepairSCCM_Click");
                        textBoxClientName.Focus();
                    }
                DirectoryCopy.Main(OptionsForm.stringSCCMRepairFolder, "\\\\" + ClientName + "\\C$\\SCCM_REPAIR", true);
                MessageBox.Show("Folder " + OptionsForm.stringSCCMRepairFolder + " copied to " + ClientName + " successfully.");
                LogFile.LogEvent(ClientName, 0, "", "", "RPR", "Folder " + OptionsForm.stringSCCMRepairFolder + " copied to " + ClientName + " successfully.");
            }

            //Initiate the process on client
            string ClientName2 = textBoxClientName.Text.ToUpper();
            var Command = OptionsForm.stringClientRepairCommand;
            var NameSpaceToRun = "\\" + ClientName2 + "\\root\\cimv2";
            var processToRun = new[] {Command}; // "C:\SCCM_REPAIR\SCCM_Repair.cmd"
            var connection = new ConnectionOptions();
            connection.Username = WindowsIdentity.GetCurrent().Name;
            connection.Password = password;
            connection.Impersonation = ImpersonationLevel.Impersonate;
            
            //MessageBox.Show("Current name: " + WindowsIdentity.GetCurrent().Name);
            var wmiScope = new ManagementScope(NameSpaceToRun, connection);
            var wmiProcess = new ManagementClass(wmiScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());



            var ExitStatus = wmiProcess.InvokeMethod("Create", processToRun);
            
            
            
            MessageBox.Show("Exit Status = " + ExitStatus.ToString());
 

            //Logging
            if (ExitStatus.ToString() == "1")
            {
                LogFile.LogEvent(ClientName2, 0, "", "", "RPR", OptionsForm.stringClientRepairCommand + " ran successfully.");
            }
            else if (ExitStatus.ToString() != "1")
            {
                LogFile.LogEvent(ClientName2, 0, "", "", "ERR", OptionsForm.stringClientRepairCommand + " did not run successfully.");
            }

        }

        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            Properties.Settings.Default.Save();
        }

        private void buttonViewLog_Click(object sender, EventArgs e)
        {
            WindowTitle = "Log Viewer - " + WindowProgramTitle;
            LogViewerForm frm = new LogViewerForm();
            frm.Show();
        }

        public static void MultiResult(string ClientName, string PkgName, string Details, bool Sucessful)
        {
            string ConCatString = ClientName + "," + PkgName +"," + Details + "," + Sucessful;
            ClientListResults.Add(ConCatString);
        }

        public static void ShowMultiResults()
        {
            MultiClientResultForm frm = new MultiClientResultForm();
            WindowTitle = "Multi-client Results - " + WindowProgramTitle;
            frm.Show();
        }
    }
}